/*
This file is part of buco.  
Copyright (c) 2012 Christopher Pawelzik <christopher@cpawelzik.com>

Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php 
*/ 
package buco.core.api;

public class ImportPackageRequirement extends Requirement {
	private String packageName;
	private VersionRange packageVersionRange; 
	private boolean isOptional; 
	
	public String getPackageName() {
		return packageName;
	}

	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}

	@Override
	public boolean isSatisfiedBy(Bundle bundle, Capability capability) {
		if(!(capability instanceof ExportPackageCapability))
			return false;
		
		ExportPackageCapability epc = (ExportPackageCapability) capability;
		
		boolean matchPackageVersions = true; 
		if(this.packageVersionRange != null && epc.getPackageVersion() != null) {
			matchPackageVersions =  this.packageVersionRange.matchesVersion(epc.getPackageVersion());
		}
		
		return matchPackageVersions && this.getPackageName().equals(epc.getPackageName());  
	}
	
	/**
	 * @return true, if the package is a system package (java.*, javax.*). These imports are always resolved.
	 */
	public boolean isSystemPackage() {
		return packageName.startsWith("java.") || packageName.startsWith("javax.");
	}

	public VersionRange getPackageVersionRange() {
		return packageVersionRange;
	}

	public void setPackageVersionRange(VersionRange packageVersionRange) {
		this.packageVersionRange = packageVersionRange;
	} 
	
	@Override
	public void accept(MetadataVisitor visitor) {
		visitor.visit(this);		
	} 
}
